/*
*  glogin.c - Login Window
*  Copyright (c) 2005 Derek Ingrouville (dergro@users.sourceforge.net)
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Library General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <string.h>

#include "glogin.h"

#include "gdialog.h"
#include "eventproc.h"
#include "msn/notification.h"

void draw_login_window (GLoginDialog *glogin);
void on_login_clicked (GtkWidget * widget, gpointer data);
void on_cancel_clicked (GtkWidget * widget, gpointer data);

/* ---------- Initialization ---------- */

GLoginDialog *
glogin_dialog_new ()
{
	GLoginDialog *glogin;
	glogin = g_new (GLoginDialog, 1);
	draw_login_window (glogin);
	return glogin;
}

void 
glogin_dialog_free (GLoginDialog *glogin)
{
	g_return_if_fail (glogin != NULL);

	gtk_widget_destroy ((GtkWidget *) glogin->dialog);
	glogin->dialog = NULL;
	glogin->acct_entry = NULL;
	glogin->pass_entry = NULL;

	return;
}

/* ---------- UI Draw ---------- */

void
draw_login_window (GLoginDialog *glogin)
{
	GtkDialog *dialog;    /* Login window */
	GtkWidget *vbox;      /* Vbox for entries */
	GtkWidget *hbox;      /* Hbox for buttons */
	GtkWidget *label;     /* Labels */
	GtkEntry *acct_entry; /* Account text entry  */
	GtkEntry *pass_entry; /* Password text entry */
	GtkWidget *button;    /* Login/Cancel Button */
	GtkWidget *align;     /* Alignment */

	dialog = (GtkDialog *) gtk_dialog_new ();
	hbox = gtk_hbox_new (FALSE, 0);
	vbox = ((GtkDialog *) dialog)->vbox;

	/* Account Label */
	label = gtk_label_new ("Account:");
	align = gtk_alignment_new (0, 0, 0, 0);
	gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);
	gtk_container_add (GTK_CONTAINER (align), label);

	/* Account entry. */
	acct_entry = (GtkEntry *) gtk_entry_new ();
	gtk_box_pack_start (GTK_BOX (vbox), (GtkWidget *) acct_entry, TRUE, TRUE, 0);
	gtk_entry_set_text ((GtkEntry *) acct_entry, "programtesting@hotmail.com");

	/* Password Label */
	label = gtk_label_new ("Password:");
	align = gtk_alignment_new (0, 0, 0, 0);
	gtk_container_add (GTK_CONTAINER (align), label);
	gtk_box_pack_start (GTK_BOX (vbox), align, TRUE, TRUE, 0);

	/* Password entry. */
	pass_entry = (GtkEntry *) gtk_entry_new ();
	gtk_entry_set_visibility ((GtkEntry *) pass_entry, FALSE);
	gtk_box_pack_start (GTK_BOX (vbox), (GtkWidget *) pass_entry, TRUE, TRUE, 0);
	gtk_entry_set_text ((GtkEntry *) pass_entry, "jkopjkoblol");

	/* Add the hbox */
	gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);

	/* Login button. */
	button = gtk_dialog_add_button ((GtkDialog *) dialog, "Login", GTK_RESPONSE_NONE);

	/* Cancel button. */
	gtk_dialog_add_button ((GtkDialog *) dialog, "Cancel", GTK_RESPONSE_NONE);

	/* Set size and position */
	gtk_widget_set_size_request ((GtkWidget *) dialog, 215, 125);
	gtk_window_set_resizable ((GtkWindow *) dialog, FALSE);
	gtk_window_set_position ((GtkWindow *) dialog, GTK_WIN_POS_CENTER);

	/* Setup callbacks */
	g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_login_clicked), glogin);
	g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_cancel_clicked), NULL);

	glogin->dialog = dialog;
	glogin->acct_entry = acct_entry;
	glogin->pass_entry = pass_entry;

	gtk_widget_show_all ((GtkWidget *) dialog);

	return;
}

int
glogin_dialog_show (GLoginDialog *glogin)
{
	gint result = 0;
	result = gtk_dialog_run ((GtkDialog *) glogin->dialog);
	return result;
}

/* ---------- UI Callbacks ---------- */

void
on_login_clicked (GtkWidget *widget, gpointer data)
{
	GLoginDialog *glogin = NULL;
	const char *acct = NULL;
	const char *pass = NULL;
	static const char *err = "Please enter both your account, and password.";

	glogin = (GLoginDialog *) data;

	if (glogin == NULL)
		return;

	acct = gtk_entry_get_text ((GtkEntry *) glogin->acct_entry);
	pass = gtk_entry_get_text ((GtkEntry *) glogin->pass_entry);

	if (acct == NULL || pass == NULL) {
		vobri_display_message (NULL, err);
		return;
	}

	if (!strlen (acct) || !strlen (pass)) {
		vobri_display_message (NULL, err);
		return;
	}

	gtk_dialog_response (glogin->dialog, GTK_RESPONSE_OK);
}

void
on_cancel_clicked (GtkWidget *widget, gpointer data)
{
	gtk_widget_destroy (widget);
}

/* ---------- Procedures ---------- */

gboolean
glogin_signin (MsnSession *session)
{
	int ret = 0;
	g_return_val_if_fail (session != NULL, FALSE);

	msn_connection_set_connect_cb (session->notif->conn, on_connection_established);
	msn_connection_set_disconnect_cb (session->notif->conn, on_disconnected);

	ret = msn_session_connect (session, "messenger.hotmail.com", 1863);
	if (ret < 0) {
		vobri_display_error (NULL, "Unable to connect to notification server.");
		return FALSE;
	}

	setup_event_handlers (session->notif->conn->callbacks);

	ret = msn_notification_start_login (session->notif);
	if (ret < 0) {
		vobri_display_error (NULL, "Unable to Sign In.");
		return FALSE;
	}

	return TRUE;
}
